// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraWorldSettings.h"
#include "GameFramework/PlayerStart.h"
#include "EngineUtils.h"
#include "Misc/UObjectToken.h"
#include "Logging/MessageLog.h"
#include "LyraLogChannels.h"
#include "Engine/AssetManager.h"

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraWorldSettings)

/**
 * 构造函数
 */
ALyraWorldSettings::ALyraWorldSettings(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
}

/**
 * 获取默认游戏玩法经验
 * @return 默认游戏玩法经验ID
 */
FPrimaryAssetId ALyraWorldSettings::GetDefaultGameplayExperience() const
{
	FPrimaryAssetId Result; // 结果
	if (!DefaultGameplayExperience.IsNull()) // 如果默认游戏玩法经验不为空
	{
		Result = UAssetManager::Get().GetPrimaryAssetIdForPath(DefaultGameplayExperience.ToSoftObjectPath()); // 获取路径的主要资源ID

		if (!Result.IsValid()) // 如果ID无效
		{
			UE_LOG(LogLyraExperience, Error, TEXT("%s.DefaultGameplayExperience is %s but that failed to resolve into an asset ID (you might need to add a path to the Asset Rules in your game feature plugin or project settings"), // 记录错误
				*GetPathNameSafe(this), *DefaultGameplayExperience.ToString());
		}
	}
	return Result; // 返回结果
}

#if WITH_EDITOR
/**
 * 检查错误
 */
void ALyraWorldSettings::CheckForErrors()
{
	Super::CheckForErrors(); // 调用父类

	FMessageLog MapCheck("MapCheck"); // 地图检查消息日志

	for (TActorIterator<APlayerStart> PlayerStartIt(GetWorld()); PlayerStartIt; ++PlayerStartIt) // 遍历玩家起始点
	{
		APlayerStart* PlayerStart = *PlayerStartIt; // 获取玩家起始点
		if (IsValid(PlayerStart) && PlayerStart->GetClass() == APlayerStart::StaticClass()) // 如果有效且是普通APlayerStart
		{
			MapCheck.Warning() // 添加警告
				->AddToken(FUObjectToken::Create(PlayerStart)) // 添加对象令牌
				->AddToken(FTextToken::Create(FText::FromString("is a normal APlayerStart, replace with ALyraPlayerStart."))); // 添加文本令牌
		}
	}

	//@TODO: 确保软对象路径实际上可以转换为主要资源ID（例如，不指向未扫描目录中的经验）
}
#endif